package com.cls.web;

import com.charles.entity.AppExportRecord;
import com.charles.event.ExportEvent;
import com.charles.mapper.AppExcelRecordMapper;
import com.charles.utils.EasyExcelUtils;
import com.cls.entity.AppTask;
import com.cls.excel.AppTaskExcel;
import com.cls.mapper.AppTaskMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

/**
 * @author charles
 * @date 2023/5/14 22:48
 */
@RestController
public class ExportController {

    @Autowired
    private AppTaskMapper taskMapper;

    @Autowired
    private AppExcelRecordMapper excelRecordMapper;

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/t1")
    public void t1() {
        AppTask appTask = taskMapper.selectById(1);
        System.out.println(appTask);
        AppExportRecord appExportRecord = excelRecordMapper.selectById(1);
        System.out.println(appExportRecord);
    }

    @GetMapping("/t2")
    public void t2() {
        AppTask appTask = taskMapper.selectById(1);
        AppTaskExcel appTaskExcel = new AppTaskExcel();
        BeanUtils.copyProperties(appTask, appTaskExcel);
        applicationContext.publishEvent(new ExportEvent<>("", "任务导出", "任务导出", AppTaskExcel.class, Arrays.asList(appTaskExcel)));
    }

    @GetMapping("/t3")
    public ModelAndView t3() {
        AppTask appTask = taskMapper.selectById(1);
        AppTaskExcel appTaskExcel = new AppTaskExcel();
        BeanUtils.copyProperties(appTask, appTaskExcel);
        return EasyExcelUtils.export("任务导出", "任务导出", AppTaskExcel.class, Arrays.asList(appTaskExcel));
    }
}
